package com.excilys.gwtfirst.domain.model;

import static com.excilys.gwtfirst.domain.model.constraints.Condition.Type.LC;
import static com.excilys.gwtfirst.domain.model.constraints.Condition.Type.NUM;
import static com.excilys.gwtfirst.domain.model.constraints.Condition.Type.UC;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.joda.time.DateTime;

import com.excilys.gwtfirst.domain.model.constraints.Condition;
import com.excilys.gwtfirst.domain.model.constraints.UserPassword;

@Entity
@Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = "LOGIN"))
public class User implements Serializable {
	private static final long serialVersionUID = 2147409168581105877L;

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Attributs

	private Integer id;
	private String fullname;
	private String login;
	private String password;
	private String email;
	private DateTime register = new DateTime();
	private Boolean admin = new Boolean(false);

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Constructeurs

	public User(String fullname, String login, String password, String email) {
		this.fullname = fullname;
		this.password = password;
		this.login = login;
		this.email = email;
	}

	public User() {

	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// toString

	@Override
	public String toString() {
		return fullname;
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Hashcode & Equals

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((fullname == null) ? 0 : fullname.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (fullname == null) {
			if (other.fullname != null)
				return false;
		} else if (!fullname.equals(other.fullname))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		return true;
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Getters annotés JPA

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}

	@NotEmpty
	@Column(name = "fullname")
	public String getFullname() {
		return fullname;
	}

	@NotNull
	@Length(min = 3, max = 20)
	@Pattern(regexp = "[a-zA-Z]*")
	@Column(name = "login")
	public String getLogin() {
		return login;
	}

	@NotNull
	@UserPassword(conditions = { @Condition(target = LC, nb = 3), //
			@Condition(target = UC, nb = 1), //
			@Condition(target = NUM, nb = 1) })
	@Length(min = 9, max = 12)
	@Pattern(regexp = "[a-zA-Z0-9]*")
	@Column(name = "password")
	public String getPassword() {
		return password;
	}

	@Email
	@NotNull
	@Column(name = "email")
	public String getEmail() {
		return email;
	}

	@Column(name = "register")
	@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
	public DateTime getRegister() {
		return register;
	}

	@Column(name = "admin")
	public Boolean isAdmin() {
		return admin;
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Setters

	public void setId(Integer id) {
		this.id = id;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setRegister(DateTime register) {
		this.register = register;
	}

	public void setAdmin(Boolean admin) {
		this.admin = admin;
	}

}
